public class Test2 {
    public static void main(String[] args) {
        int[] nums = {2,2,1,1,1,2,2};
        System.out.println(majorityElement(nums));
    }

    public static int majorityElement(int[] nums){
        int count = 1;
        int tmp = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (tmp == nums[i]){
                count++;
            }
            else {
                count--;
                if (count == 0){
                    tmp = nums[i+1];
                }
            }
        }
        return tmp;
    }
}


